﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;

namespace WSMP.WSMPDataAccess
{


    public class DataAccess
    {
        #region Public variables
        public string Strcon;
        DataSet dts = new DataSet();
        public DataAccess()
        {
            Strcon = sReadConnectionString();

        }
        public string sReadConnectionString()
        {
            try
            {

                ConnectionStringSettingsCollection connectionStrings =
                     ConfigurationManager.ConnectionStrings;

                foreach (ConnectionStringSettings connection in connectionStrings)
                {
                    if (connection.Name == "ApplicationServices")
                    {
                        Strcon = connection.ToString();
                        break;
                    }
                }
            }

            finally
            {
            }
            return Strcon;
        }

        public SqlConnection connection;
        public SqlCommand cmd;
        public SqlDataAdapter adpt;
        public DataTable dt;
        public int intresult;
        public SqlDataReader sqdr;
        #endregion
        #region Public Methods
        public DataTable getDataUsingSP(string spName)
        {
            return getDataUsingSP(spName, null);
        }

        public DataTableAndValue getDataUsingSP(string spName, SPParamCollection spParamCollection
            , SPParamReturnCollection spParamReturnCollection
            )
        {
            try
            {
                //spParamReturned = null;
                SPParamReturnCollection spParamReturned = new SPParamReturnCollection();
                using (connection = new SqlConnection(Strcon))
                {
                    connection.Open();
                    using (cmd = new SqlCommand(spName, connection))
                    {

                        if (spParamCollection != null)
                        {
                            foreach (SPParams _SPParams in spParamCollection)
                            {
                                cmd.Parameters.AddWithValue(_SPParams.Name, _SPParams.Value);
                            }
                        }
                        if (spParamReturnCollection != null)
                        {
                            foreach (SPParams paramReturn in spParamReturnCollection)
                            {
                                SqlParameter _parmReturn = new SqlParameter(paramReturn.Name, paramReturn.Value);
                                _parmReturn.Direction = paramReturn.ParamDirection;
                                if (paramReturn.Size > 0) _parmReturn.Size = paramReturn.Size;
                                else _parmReturn.Size = 32;
                                _parmReturn.SqlDbType = paramReturn.Type;
                                cmd.Parameters.Add(_parmReturn);
                            }
                        }
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandTimeout = 60;
                        adpt = new SqlDataAdapter(cmd);
                        dt = new DataTable();
                        adpt.Fill(dt);
                        connection.Close();
                        if (spParamReturnCollection != null)
                        {
                            if (spParamReturned == null)
                                spParamReturned = new SPParamReturnCollection();
                            for (int i = 0; i < spParamReturnCollection.Count; i++)
                            {
                                spParamReturned.Add(new SPParams
                                {
                                    Name = spParamReturnCollection[i].Name,
                                    Value = cmd.Parameters[spParamReturnCollection[i].Name].Value

                                });

                            }
                        }
                        return (new DataTableAndValue() { DataTable = dt, SPParamReturnCollection = spParamReturned });
                    }
                }
            }
            finally
            {
                connection.Close();
            }

        }
        public DataTable getDataUsingSP(string spName, SPParamCollection spParamCollection)
        {
            return getDataUsingSP(spName, spParamCollection, null).DataTable;
        }

        public DataSet getDataSetUsingSP(string spName)
        {
            return getDataSetUsingSP(spName, null);
        }
        public DataSet getDataSetUsingSP(string spName, SPParamCollection spParamCollection)
        {
            try
            {
                using (connection = new SqlConnection(Strcon))
                {
                    connection.Open();
                    using (cmd = new SqlCommand(spName, connection))
                    {
                        int count, param = 0;
                        if (spParamCollection == null)
                        {
                            param = -1;
                        }
                        else
                        {
                            param = spParamCollection.Count;
                        }
                        for (count = 0; count < param; count++)
                        {
                            cmd.Parameters.AddWithValue(spParamCollection[count].Name, spParamCollection[count].Value);
                        }
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandTimeout = 60;
                        adpt = new SqlDataAdapter(cmd);
                        DataSet ds = new DataSet();
                        adpt.Fill(ds);
                        return ds;
                    }
                }
            }
            finally
            {
                connection.Close();
            }

        }
        public SqlDataReader getDataReaderUsingSP(string spName)
        {
            return getDataReaderUsingSP(spName, null);
        }
        public SqlDataReader getDataReaderUsingSP(string spName, SPParamCollection spParamCollection)
        {

            try
            {
                using (connection = new SqlConnection(Strcon))
                {
                    connection.Open();
                    using (cmd = new SqlCommand(spName, connection))
                    {
                        int count, param = 0;
                        if (spParamCollection == null)
                        { param = -1; }
                        else
                        {
                            param = spParamCollection.Count;
                        }
                        for (count = 0; count < param; count++)
                        {
                            cmd.Parameters.AddWithValue(spParamCollection[count].Name, spParamCollection[count].Value);
                        }
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandTimeout = 60;
                        sqdr = cmd.ExecuteReader();

                        return (sqdr);
                    }
                }
            }
            finally
            {
                connection.Close();
            }
        }
        public int executeSP(string spName)
        {
            return executeSP(spName, null);
        }
        public int executeSP(string spName, SPParamCollection spParamCollection, bool addExtraParmas)
        {
            try
            {

                using (connection = new SqlConnection(Strcon))
                {
                    connection.Open();
                    using (cmd = new SqlCommand(spName, connection))
                    {
                        int count, param = 0;
                        if (spParamCollection == null)
                        { param = -1; }
                        else
                        {
                            param = spParamCollection.Count;
                        }
                        for (count = 0; count < param; count++)
                        {
                            SqlParameter par = new SqlParameter(spParamCollection[count].Name, spParamCollection[count].Value);
                            if (addExtraParmas)
                            {
                                par.TypeName = spParamCollection[count].TypeName;
                                par.SqlDbType = spParamCollection[count].Type;
                            }
                            cmd.Parameters.Add(par);
                        }
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandTimeout = 60;
                        return (cmd.ExecuteNonQuery());
                    }
                }
            }

            finally
            {
                connection.Close();
            }

        }
        public int executeSP(string spName, SPParamCollection spParamCollection)
        {

            return executeSP(spName, spParamCollection, false);

        }

        public DataTable getDataUsingSqlQuery(string strSqlQuery)
        {

            try
            {
                using (connection = new SqlConnection(Strcon))
                    connection.Open();
                {
                    using (cmd = new SqlCommand(strSqlQuery, connection))
                    {
                        cmd.CommandType = CommandType.Text;
                        cmd.CommandTimeout = 60;
                        adpt = new SqlDataAdapter(cmd);
                        dt = new DataTable();
                        adpt.Fill(dt);
                        return (dt);
                    }
                }
            }
            finally
            {
                connection.Close();
            }

        }
        public int executeSqlQuery(string strSqlQuery)
        {

            try
            {
                using (connection = new SqlConnection(Strcon))
                {
                    connection.Open();
                    using (cmd = new SqlCommand(strSqlQuery, connection))
                    {
                        cmd.CommandType = CommandType.Text;
                        cmd.CommandTimeout = 60;
                        intresult = cmd.ExecuteNonQuery();
                        return (intresult);
                    }
                }
            }
            finally
            {
                connection.Close();
            }

        }
        public SPParamReturnCollection executeSPReturnParam(string spName, SPParamReturnCollection spParamReturnCollection)
        {
            return executeSPReturnParam(spName, null, spParamReturnCollection);
        }
        public int executeSPReturnParam()
        {
            return 0;
        }

        public int executeSPReturnParam(string spName, SPParamCollection spParamCollection, ref SPParamReturnCollection spParamReturnCollection)
        {
            try
            {
                SPParamReturnCollection spParamReturned = new SPParamReturnCollection();
                using (connection = new SqlConnection(Strcon))
                {
                    connection.Open();
                    using (cmd = new SqlCommand(spName, connection))
                    {
                        int count, param = 0;
                        if (spParamCollection == null)
                        { param = -1; }
                        else
                        {
                            param = spParamCollection.Count;
                        }
                        for (count = 0; count < param; count++)
                        {
                            cmd.Parameters.AddWithValue(spParamCollection[count].Name, spParamCollection[count].Value);
                        }
                        cmd.CommandType = CommandType.StoredProcedure;
                        foreach (SPParams paramReturn in spParamReturnCollection)
                        {
                            SqlParameter _parmReturn = new SqlParameter(paramReturn.Name, paramReturn.Size);
                            _parmReturn.Direction = paramReturn.ParamDirection;
                            if (paramReturn.Size > 0) _parmReturn.Size = paramReturn.Size;
                            else _parmReturn.Size = 32;
                            _parmReturn.SqlDbType = paramReturn.Type;
                            cmd.Parameters.Add(_parmReturn);
                        }
                        cmd.CommandTimeout = 60;
                        intresult = cmd.ExecuteNonQuery();
                        connection.Close();

                        //for (int i = 0; i < spParamReturnCollection.Count; i++)
                        //{
                        //    spParamReturned.Add(new SPParams
                        //    {
                        //        Name = spParamReturnCollection[i].Name,
                        //        Value = cmd.Parameters[spParamReturnCollection[i].Name].Value
                        //    });

                        //}
                    }
                }
                return intresult;
            }
            finally
            {
                connection.Close();

            }
        }
        public SPParamReturnCollection executeSPReturnParam(string spName, SPParamCollection spParamCollection, SPParamReturnCollection spParamReturnCollection)
        {
            return executeSPReturnParam(spName, spParamCollection, spParamReturnCollection, false);
        }
        public SPParamReturnCollection executeSPReturnParam(string spName, SPParamCollection spParamCollection, SPParamReturnCollection spParamReturnCollection, bool addExtraParmas)
        {
            try
            {
                SPParamReturnCollection spParamReturned = new SPParamReturnCollection();
                using (connection = new SqlConnection(Strcon))
                {
                    connection.Open();
                    using (cmd = new SqlCommand(spName, connection))
                    {
                        int count, param = 0;
                        if (spParamCollection == null)
                        { param = -1; }
                        else
                        {
                            param = spParamCollection.Count;
                        }
                        for (count = 0; count < param; count++)
                        {
                            //cmd.Parameters.AddWithValue(spParamCollection[count].Name, spParamCollection[count].Value);
                            SqlParameter par = new SqlParameter(spParamCollection[count].Name, spParamCollection[count].Value);
                            if (addExtraParmas)
                            {
                                par.TypeName = spParamCollection[count].TypeName;
                                par.SqlDbType = spParamCollection[count].Type;
                            }
                            cmd.Parameters.Add(par);
                        }
                        cmd.CommandType = CommandType.StoredProcedure;
                        foreach (SPParams paramReturn in spParamReturnCollection)
                        {
                            SqlParameter _parmReturn = new SqlParameter(paramReturn.Name, paramReturn.Value);
                            _parmReturn.Direction = paramReturn.ParamDirection;
                            if (paramReturn.Size > 0) _parmReturn.Size = paramReturn.Size;
                            else _parmReturn.Size = 32;
                            _parmReturn.SqlDbType = paramReturn.Type;
                            cmd.Parameters.Add(_parmReturn);
                        }
                        cmd.CommandTimeout = 60;
                        cmd.ExecuteNonQuery();
                        connection.Close();

                        for (int i = 0; i < spParamReturnCollection.Count; i++)
                        {
                            spParamReturned.Add(new SPParams
                            {
                                Name = spParamReturnCollection[i].Name,
                                Value = cmd.Parameters[spParamReturnCollection[i].Name].Value

                            });

                        }
                    }
                }
                return spParamReturned;
            }
            finally
            {
                connection.Close();
            }
        }
        public object getScalarUsingSP(string spName)
        {
            return getScalarUsingSP(spName, null);
        }
        public object getScalarUsingSP(string spName, SPParamCollection spParamCollection)
        {
            try
            {
                using (connection = new SqlConnection(Strcon))
                {
                    connection.Open();
                    using (cmd = new SqlCommand(spName, connection))
                    {
                        int count, param = 0;
                        if (spParamCollection == null)
                        { param = -1; }
                        else
                        {
                            param = spParamCollection.Count;
                        }
                        for (count = 0; count < param; count++)
                        {
                            cmd.Parameters.AddWithValue(spParamCollection[count].Name, spParamCollection[count].Value);

                        }
                        cmd.CommandTimeout = 60;
                        cmd.CommandType = CommandType.StoredProcedure;
                        return cmd.ExecuteScalar();
                    }
                }
            }
            finally
            {
                connection.Close();
                cmd.Dispose();
            }
        }
        #endregion
    }
}
